<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class AdminsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view("admin.admins.index");
    }
    public function getadmins(Request $request){
        $input = $request->input();
        $start = $input['page']*$input['limit']-$input['limit'];
        $end = $input['page']*$input['limit'];
        $data = [
            'code'=>0,
            'msg'=>'',
            'count'=>DB::table("admins")->count(),
            'data'=>DB::table("admins")->offset($start)->limit($end)->get()
        ];
        return $data;
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $roles = DB::table("roles")->get();
        return view("admin.admins.add",compact('roles'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->input();
        $admin_data = [
            'name'=>$input['name'],
            'email'=>$input['email'],
            'updated_at'=>date("Y-m-d H:i:s"),
            'created_at'=>date("Y-m-d H:i:s"),
            'password' => bcrypt($input['password']),
        ];
        $re = DB::table("admins")->insertgetid($admin_data);
        if($re){
            if($input['roles']){
                foreach ($input['roles'] as $v){
                    DB::table("role_admin")->insert(['role_id'=>$v,'admin_id'=>$re]);
                }
            }
            return view("welcome",['id'=>1,'msg'=>'添加成功']);
        }else{
            return view("welcome",['id'=>2,'msg'=>'添加失败']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $roles = DB::table("roles")->get();
        $data = DB::table("admins")->where("id",$id)->first();
        return view("admin.admins.edit",['data'=>$data,'roles'=>$roles]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = $request->input();
        $admin_data = [
            'name'=>$input['name'],
            'email'=>$input['email'],
            'updated_at'=>date("Y-m-d H:i:s"),
        ];
        if($input['password']){
            $admin_data['password'] = bcrypt($input['password']);
        }
        $re = DB::table("admins")->where("id",$id)->update($admin_data);
        if($re){
            DB::table("role_admin")->where("admin_id",$id)->delete();
            if($input['roles']){
                foreach ($input['roles'] as $v){
                    DB::table("role_admin")->insert(['role_id'=>$v,'admin_id'=>$id]);
                }
            }
            return view("welcome",['id'=>1,'msg'=>'编辑成功']);
        }else{
            return view("welcome",['id'=>2,'msg'=>'编辑失败']);
        }
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if($id == 1){
            return 2;
        }
        DB::table("role_admin")->where("admin_id",$id)->delete();
        DB::table("admins")->where("id",$id)->delete();
        return 1;
    }
}
